package com.example.mybaitsdemo.dao;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;


import java.io.IOException;
import java.io.Reader;

public class DBAccess {
    public SqlSession getSqlSession() throws IOException{
        //通过配置文件读取数据库俩连接信息
        String resourse = "MyBatis-config.xml";
        Reader reader= Resources.getResourceAsReader(resourse);
        //通过配置信息构建sqlsessionfactory
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
        //通过sqlsssionfactory打开数据库会话
        SqlSession sqlSession=sqlSessionFactory.openSession();
        return  sqlSession;
    }
}
